using System;
using System.Xml;

namespace Mbs.CastleContainer
{
    class AttributesElementProcessor : AbstractXmlNodeProcessor
    {
        public override String Name
        {
            get { return "attributes"; }
        }


        public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine)
        {
            XmlElement element = nodeList.Current as XmlElement;

            DefaultXmlProcessorNodeList childNodes = new DefaultXmlProcessorNodeList(element.ChildNodes);

            while (childNodes.MoveNext())
            {
                engine.DispatchProcessCurrent(childNodes);

                if (IgnoreNode(childNodes.Current)) continue;

                XmlElement elem = GetNodeAsElement(element, childNodes.Current);

                AppendElementAsAttribute(element.ParentNode, childNodes.Current as XmlElement);
            }

            RemoveItSelf(element);
        }

        protected void AppendElementAsAttribute(XmlNode parentElement, XmlElement element)
        {
            XmlAttribute attribute = parentElement.OwnerDocument.CreateAttribute(element.Name);

            attribute.Value = element.InnerText;

            parentElement.Attributes.Append(attribute);
        }
    }
}
